var home = require("../../utils/util.js");
var url = home.home;
var url2 = home.home2;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopId:'1485853192971051010',
    userId:home.getKey('userId'),//会员id
    stockId:'1494987016343633921',//股东id
    bgcolor: home.getKey('bgcolor'),
    stockPhoto: home.getKey('stockPhoto'),
    gdData:{
      equityBalance:0,//剩余股本
      totalDividend:0,//累计分红
      memberQty:0,//客源数量
    },//股东数据
    status:home.getKey('status'),
    phone:home.getKey('phone'),
    avatar:home.getKey('avatar'),
    username:home.getKey('nickName'),
    balance:home.getKey('balance'),
    discount:home.getKey('discount'),
    levelName:home.getKey('levelName'),
    memberType:home.getKey('memberType'),
    state: wx.getStorageSync('state'),
    states: false,
    btnState:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      states:false,
      state: wx.getStorageSync('state'),
    });
  },

  /**
   * 跳转个人信息
   */
  toUserInfo(){
    wx.navigateTo({
      url: '../../shop/userInfo/index',
    });
  },

  /**
   * 跳转已购项目
   */
  payPorject(){
    wx.navigateTo({
      url: '../../shop/project/pay',
    })
  },

  /**
   * 跳转我的预约
   */
  payYuyue(){
    wx.navigateTo({
      url: '../../shop/yuyue/pay',
    })
  },

  /**
   * 跳转已购商品
   */
  payGoods(){
    wx.navigateTo({
      url: '../../shop/goods/pay',
    })
  },

  /**
   * 跳转提现
   */
  toWith(){
    wx.navigateTo({
      url: '../../user/share/with',
    })
  },

  /**
   * 跳转股本明细
   */
  toEquity(){
    wx.navigateTo({
      url: '../../user/share/equity',
    })
  },

  /**
   * 跳转分红明细
   */
  toBonus(){
    wx.navigateTo({
      url: '../../user/share/bonus',
    })
  },

  /**
   * 跳转我的客源
   */
  toTourist(){
    wx.navigateTo({
      url: '../../user/share/tourist',
    })
  },

  /**
   * 跳转提现记录
   */
  toDrawal(){
    wx.navigateTo({
      url: '../../user/share/drawal',
    })
  },

  /**
   * 跳转订单列表
   */
  toOrder(e){
    wx.navigateTo({
      url: '../../user/order/index?type='+e.currentTarget.dataset.type,
    })
  },
  /**
   * 跳转升级股东
   */
  toUpdate(){
    wx.navigateTo({
      url: '../../user/share/gudong',
    })
  },

  /**
   * 跳转余额
   */
  toYue(){
    wx.navigateTo({
      url: '../../user/share/balance',
    })
  },

  /**
   * 跳转贵宾卡
   */
  toGuest(){
    wx.navigateTo({
      url: '../../shop/guest/index',
    })
  },

  /**
   * 跳转拓客卡
   */
  toToker(){
    wx.navigateTo({
      url: '../../user/toker/index',
    })
  },

  //获取用户数据
  getUserInfos(){
    let that = this;
    wx.request({
      url: url + 'app/getUserInfo',
      data:{
        storeId:home.getKey('storeId'),
        phone:home.getKey('phone'),
        xcxOpenId:home.getKey('openId'),
      },
      method: 'GET',
      header: {
        "content-type": "application/json", // 默认
      },
      success:function (res) {
        if (res.data.code == 200) {
          if (res.data.result.discount == null) {
            res.data.result.discount = 10
          }
          if (res.data.result.balance == null) {
            res.data.result.balance = 0;
          }
          if (res.data.result.stockLowest == null) {
            res.data.result.stockLowest = 0;
          }
          if (res.data.result.equityBalance == null) {
            res.data.result.equityBalance = 0;
          }
          if (res.data.result.totalDividend == null) {
            res.data.result.totalDividend = 0;
          }
          if (res.data.result.equityTotal == null) {
            res.data.result.equityTotal = 0;
          }
          that.setData({
            balance:res.data.result.balance,
            levelName:res.data.result.levelName,
            discount:res.data.result.discount,
            totalDividend:res.data.result.totalDividend,
            stockMemberCount:res.data.result.stockMemberCount,
            equityBalance:res.data.result.equityBalance,
            vipCount:res.data.result.vipCount,
            username:res.data.result.nickName,
            status:res.data.result.status,
            memberType:res.data.result.memberType,
            avatar:res.data.result.headProtrait
          });
          home.setKey('memberType',res.data.result.memberType);
          home.setKey('userId',res.data.result.id);
          home.setKey('discount',res.data.result.discount);
          home.setKey('status',res.data.result.status);
          home.setKey('equityBalance',res.data.result.equityBalance);
          home.setKey('balance',res.data.result.balance);
          home.setKey('levelName',res.data.result.levelName);
          home.setKey('stockLowest',res.data.result.stockLowest);
          home.setKey('stockId',res.data.result.stockId);
          home.setKey('birthday',res.data.result.birthday);
          home.setKey('equityTotal',res.data.result.equityTotal);
          home.setKey('sex', res.data.result.sexName);
          home.setKey('nickName', res.data.result.nickName);
          home.setKey('canWithdrawal',res.data.result.canWithdrawal);
          home.setKey('noUseEquityBalancd',res.data.result.noUseEquityBalancd);
          home.setKey('avatar',res.data.result.headProtrait);
          home.setKey('totalDividend',res.data.result.totalDividend);
        }
      }
    })
  },
   //小程序授权
   login() {
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      btnState:false,
    });
    let that = this;
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: function (ress) {
        wx.login({
          success(res) {
            wx.request({
              url: 'https://kyz.fbhkj.com/fubaihui/app/xcx/updateUserNickName?storeId=' + app.globalData.storeId + '&code=' + res.code + '&xcxOpenId=' + wx.getStorageSync('openId') + '&unionId=' + wx.getStorageSync('unionId') + '&nickName' + ress.userInfo.nickName + '&headPortrait=' + ress.userInfo.avatarUrl + '&sex=保密',
              data: {
                storeId: app.globalData.storeId,
                nickName: ress.userInfo.nickName,
                headPortrait: ress.userInfo.avatarUrl,
                sex: '保密',
                xcxOpenId: wx.getStorageSync('openId'),
                unionId: wx.getStorageSync('unionId')
              },
              method: 'POST',
              header: {
                "content-type": "application/json", // 默认
              },
              success: function (rea) {
                if (rea.data.code == 200) {
                  wx.hideLoading({
                    success: (res) => {
                      that.setData({
                        btnState: true,
                        states:false,
                        state:2
                      })
                    },
                  })
                  wx.setStorageSync('nickName', ress.userInfo.nickName);
                  wx.setStorageSync('avatar', ress.userInfo.avatarUrl);
                  that.setData({
                    state: 2
                  });
                  wx.setStorageSync('state', 2);
                  wx.reLaunch({
                    url: '../user/index',
                })
                }
              }
            })
          }
        })
      },
      fail: function (res) {
        that.setData({
          btnState: true,
        })
        wx.showToast({
          title: '授权失败',
          icon: 'none',
          duration: 1500,
        })
      }
    })
  },
  register() {
    wx.showLoading({
        title: '加载中...',
    });
    let that = this;
    wx.login({
        success(res) {
            wx.request({
                url: 'https://kyz.fbhkj.com/fubaihui/app/xcx/register?storeId=' + app.globalData.storeId + '&code=' + res.code,
                data: {
                    storeId: app.globalData.storeId,
                    code: res.code
                },
                method: 'POST',
                header: {
                    "content-type": "application/json", // 默认
                },
                success: function (rea) {
                    if (rea.data.code == 200) {
                        let states = false;
                        if (!rea.data.result.hasPhone) {
                            that.setData({
                                state: 1
                            });
                            //home.setKey('state', 1);
                           // wx.setStorageSync('state', 1);
                        } else if (!rea.data.result.hasNickName) {
                            states = true;
                            that.setData({
                                state: 0
                            });
                            //home.setKey('state', 0);
                            //wx.setStorageSync('state', 0);
                        }
                        home.setKey('subscribeTencent', rea.data.result.subscribeTencent);
                        home.setKey('hasNickName', rea.data.result.hasNickName);
                        home.setKey('hasPhone', rea.data.result.hasPhone);
                        home.setKey('userId', rea.data.result.member.id);
                        home.setKey('phone', rea.data.result.member.phone);
                        home.setKey('avatar', rea.data.result.member.avatarUrl);
                        home.setKey('status', rea.data.result.member.status);
                        home.setKey('discount', rea.data.result.member.discount);
                        home.setKey('nickName', rea.data.result.member.nickName);
                        home.setKey('openId', rea.data.result.member.xcxOpenId);
                        home.setKey('unionId', rea.data.result.member.unionId);
                        let states1 = false;
                        if (rea.data.result.hasPhone && rea.data.result.hasNickName) {
                            states1 = !rea.data.result.subscribeTencent;
                            that.setData({
                                state: 2
                            });
                            home.setKey('state', 2);
                            wx.setStorageSync('state', 2);
                            that.getUserInfos();
                        }
                        that.setData({
                            states: states,
                            states1: states1,
                        })
                        wx.hideLoading({
                            success: (res) => {

                            },
                        })
                    } else {
                        home.wxMessage(rea.data.message, 'none');
                    }
                }
            })
        }
    })
},
  getPhoneNumber(e) {
    console.log(e.detail);
    wx.showLoading({
      title: '加载中...',
    });
    this.setData({
      btnState1:false,
    })
    if (e.detail.code == undefined) {
      wx.showToast({
        title: '请授权获取手机号',
        icon: 'none',
        duration: 1500,
      })
      wx.setStorageSync('state', 1);
      this.setData({
        btnState1:true,
      })
      return false;
    }
    let that = this;
    wx.request({
      url: 'https://kyz.fbhkj.com/fubaihui/app/xcx/getPhone?storeId=' + app.globalData.storeId + '&code=' + e.detail.code + '&xcxOpenId=' + wx.getStorageSync('openId') + '&unionId=' + wx.getStorageSync('unionId'),
      data: {
        storeId: app.globalData.storeId,
        code: e.detail.code,
        xcxOpenId:wx.getStorageSync('openId'),
        unionId:wx.getStorageSync('unionId')
      },
      method: 'POST',
      header: {
        "content-type": "application/json", // 默认
      },
      success: function (res) {
        if (res.data.code == 200) {
          wx.hideLoading({
            success: (res) => {
              that.setData({
                btnState1: true,
              });
            },
          })
          wx.setStorageSync('phone', res.data.result.phone);
          wx.setStorageSync('userId', res.data.result.id);
          let state = 2;
          
          if (!wx.getStorageSync('hasNickName')) {
            state = 0;
          }
          
          that.setData({
            state: state
          });
          wx.setStorageSync('state', state);
        } else {
          wx.showToast({
            title: '获取失败',
            icon: 'none',
            duration: 1500,
          })
        }
      }
    })
  },
  onClose1() {
    this.setData({
        states: false,
    })
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userId:home.getKey('userId'),
      status:home.getKey('status'),
      avatar:home.getKey('avatar'),
      username:home.getKey('nickName'),
      phone:home.getKey('phone'),
      avatar:home.getKey('avatar'),
    })
    this.getTabBar().setData({
      show3:false,
      show1:false,
      show2:false,
      show4:true,
    })
    if (home.getKey('state') == 1) {
      this.getTabBar().setData({
        state: 1
      });
    } else if (home.getKey('state') == 2) {
      this.getTabBar().setData({
        state: 2
      });
    }
    this.getUserInfos();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})